package com.base.caselib;

import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.arvin.imagepicker.bean.ImageItem;
import com.base.caselib.bean.CaseType;
import com.base.caselib.bean.PreTrialBean;
import com.base.caselib.databinding.ActivityPretrialBinding;
import com.base.caselib.presenter.PreTrialPresenter;
import com.base.caselib.view.PreTrialView;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.widget.SingleChooseDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weikailiang on 2019/10/29.
 */
@Route(path = "/pretrial/pret")
public class PreTrialActivity extends CBaseBarMvpActivity<PreTrialPresenter> implements PreTrialView{

    private ActivityPretrialBinding mBinding;

    @Autowired
    String mFormId;//表单ID
    @Autowired
    String CaseId;//案件ID
    @Autowired
    String tbFlag;//自定义表单名
    @Autowired
    String content;//表单标题
    @Autowired
    String nextCaseStatus;//下个案件状态
    @Autowired
    String mCaseAbstract;

    ArrayList<CaseType> mCaseType;



    @Override
    protected int getLayoutResource() {
        return R.layout.activity_pretrial;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityPretrialBinding) mRootBinding;
        mPresenter = new PreTrialPresenter(this,this);
        mPresenter.start();
        mPresenter.initPics(CaseId);
        setBackNavigation();
        setBarTitle("一般案件");
        mCaseType = (ArrayList<CaseType>) getIntent().getSerializableExtra("casetype");
        setRigTv("通知书", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                mPresenter.getNoticePdfUrl(CaseId,"通知书");
                mPresenter.getPdfUrlZLZG(CaseId,"责令停止违反城市管理行为通知书");
            }
        });
    }

    @Override
    protected void start() {

        mBinding.llAsk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //制作调查询问笔录
                ARouter.getInstance().build("/askrecord/ask")
                        .withString("mFormId",mFormId)
                        .withString("mCaseId",CaseId).navigation();
            }
        });
        mBinding.llChecl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //制作通知书及现场检查笔录
                ARouter.getInstance().build("/checkrecord/check")
                        .withString("mCaseId",CaseId)
                        .withString("mFormId",mFormId)
                        .withString("mCaseAbstract",mCaseAbstract)
                        .navigation();
            }
        });
        mBinding.up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCaseType == null){
                    //没有流程
                    showMes("未获取到流程");
                }else if (mCaseType.size()==1){
                    //只有一个流程
                    mPresenter.BindCase(mCaseType.get(0).getId(),CaseId,mFormId,tbFlag,content,nextCaseStatus);
                }else if (mCaseType.size()>1){
                    //多个流程让用户选择
                    SingleChooseDialog<CaseType> dialog = new SingleChooseDialog<CaseType>(mContext,"请选择流程", mCaseType, new SingleChooseDialog.OnItemChooseListener() {
                        @Override
                        public void onItemChoose(Object o) {
                            for (CaseType type : mCaseType){
                                if (type.getPreName().equals(o.toString())){
                                    mPresenter.BindCase(type.getId(),CaseId,mFormId,tbFlag,content,nextCaseStatus);
                                    return;
                                }
                            }
                        }
                    });
                    dialog.show();
                }
            }
        });
        mBinding.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                List<String> sence = new ArrayList<>();
                if (mBinding.sence.getPics()!=null&&mBinding.sence.getPics().size()>0) {
                    for (ImageItem str : mBinding.sence.getPics()) {
                        if (str.path != null) {
                            sence.add(str.path);
                        }
                    }
                }else {
                    showMes("请添加现场照片");
                    return;
                }


                List<String> notice = new ArrayList<>();
                if (mBinding.pcker1.getPics()!=null&&mBinding.pcker1.getPics().size()>0) {
                    for (ImageItem str : mBinding.pcker1.getPics()) {
                        if (str.path != null) {
                            notice.add(str.path);
                        }
                    }
                }else {
                    showMes("请添加通知书照片");
                    return;
                }
                List<String> inqu = new ArrayList<>();
                if (mBinding.pcker2.getPics()!=null&&mBinding.pcker2.getPics().size()>0) {
                    for (ImageItem str : mBinding.pcker2.getPics()) {
                        if (str.path != null) {
                            inqu.add(str.path);
                        }
                    }
                }else {
                    showMes("请添加现场检查照片");
                    return;
                }
                mPresenter.upPreTrial(sence,notice,inqu,mFormId,CaseId);
            }
        });
    }

    @Override
    public void returnSuccesSave() {
        mBinding.save.setVisibility(View.GONE);
        mBinding.up.setVisibility(View.VISIBLE);
    }

    @Override
    public void returnNext() {
        mBinding.save.setVisibility(View.GONE);
        mBinding.up.setVisibility(View.GONE);
    }

    @Override
    public void returnPreTrialBean(PreTrialBean bean) {
        if (bean.getNoticePic()!=null&&bean.getNoticePic().size()>0){
            mPresenter.mHasSave = true;
            mBinding.up.setVisibility(View.GONE);
        }
        List<ImageItem> noticeIte = new ArrayList<>();
        for (String str : bean.getNoticePic()){
            List<PreTrialBean.fileBean> fileBean1 = new Gson().fromJson(str,new TypeToken<PreTrialBean.fileBean>(){}.getType());
            for (PreTrialBean.fileBean fileBean : fileBean1) {
                if (fileBean.getContent().startsWith("http")) {
                    noticeIte.add(new ImageItem(fileBean.getContent(), ""));
                } else {
                    noticeIte.add(new ImageItem(RealNetWorkUtil.getBaseUrl() + "api-file/" + fileBean.getContent(), ""));
                }
            }
        }
        mBinding.pcker1.setPics(noticeIte);
        List<ImageItem> noticeIte2 = new ArrayList<>();
        for (String str : bean.getInspectionPic()){
            List<PreTrialBean.fileBean> fileBean2 = new Gson().fromJson(str,new TypeToken<PreTrialBean.fileBean>(){}.getType());
            for (PreTrialBean.fileBean fileBean : fileBean2) {
                if (fileBean.getContent().startsWith("http")) {
                    noticeIte2.add(new ImageItem(fileBean.getContent(), ""));
                } else {
                    noticeIte2.add(new ImageItem(RealNetWorkUtil.getBaseUrl() + "api-file/" + fileBean.getContent(), ""));
                }
            }

        }
        mBinding.pcker2.setPics(noticeIte2);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }
}
